package com.suyin.ximoon.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author TECH26
 *
 */
public class DateUtils  {
	 private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	 /** 
     * Number of milliseconds in a standard second. 
     */  
    public static final long MILLIS_PER_SECOND = 1000;// 一秒钟的毫秒数  
      
    /** 
     * Number of milliseconds in a standard minute. 
     */  
    public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;// 一分钟的毫秒数  
  
    /** 
     * Number of milliseconds in a standard hour. 
     */  
    public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;// 一小时的毫秒数  
  
    /** 
     * Number of milliseconds in a standard day. 
     */  
    public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;// 一天的毫秒数  
      
    /** 
     * ISO8601 date format: yyyy-MM-dd 
     */  
    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";  
  
    /** 
     * ISO8601 date-time format: yyyy-MM-dd HH:mm:ss 
     */  
    public static String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";  
      
    private DateUtils()  
    {  
          
    }  
      
    public static String fillZero(String str, int fillSize)  
    {  
        if (null != str)  
        {  
            int beforeSize = str.length();  
            if(str.length() < fillSize)  
            {  
                int afterSize = fillSize-beforeSize;  
                for(int i=0; i<afterSize; i++)  
                {  
                    str = "0"+str;  
                }  
            }  
        }  
          
        return str;  
    }  
  
    /** 
     * 获得系统时间yyyy-MM-dd HH:mm:ss格式 
     *  
     * @return 
     */  
    public static String getSystime() {  
        Calendar todaysDate = new GregorianCalendar();  
        int year = todaysDate.get(Calendar.YEAR);  
        int month = todaysDate.get(Calendar.MONTH) + 1;  
        int day = todaysDate.get(Calendar.DAY_OF_MONTH);  
        int hourOfDay = todaysDate.get(Calendar.HOUR_OF_DAY);//24小时制  
        // int hour = todaysDate.get(Calendar.HOUR); //12小时制  
        int minute = todaysDate.get(Calendar.MINUTE);  
        int second = todaysDate.get(Calendar.SECOND);  
          
        return year + "/" + month + "/" + day + " " + hourOfDay + ":" + fillZero(minute+"", 2)  
                + ":" + fillZero(second+"", 2);  
    }  
    /** 
     * 获得当前时间yyyy-MM-dd HH:mm:ss格式 
     * @return 
     */  
    public static String getCurrentDateAsString()  
    {  
        SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT_PATTERN);  
  
        return formatter.format(new Date());  
    }
    /** 
     * 获得当前日期yyyy-MM-dd格式 
     * @return 
     */
    public static String getDateAsString()  
    {  
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_PATTERN);  
  
        return formatter.format(new Date());  
    }
    /** 
     * 获得当前日期yyyy-MM-dd HH:mm:ss格式 
     * @return 
     */  
    public static Date getCurrentDate()  
    {  
        return parseToDate(getCurrentDateAsString(), DATETIME_FORMAT_PATTERN);  
    }  
   
    
    /** 
     * 将日期字符串转换为指定的日期格式 
     *  
     * @param str 日期字符串 
     * @param pattern 日期格式 
     * @return 
     */  
    public static Date parseToDate(String str, String pattern)  
    {  
        DateFormat parser = new SimpleDateFormat(pattern);  
        try  
        {  
            return parser.parse(str);  
        }  
        catch (ParseException e)  
        {  
            throw new IllegalArgumentException("Can't parse " + str + " using " + pattern);  
        }  
    }  
    /** 
     * 将yyyy-MM-dd HH:mm:ss格式字符串转成Date 
     * @param str 
     * @return 
     */  
    public static Date parseToDate(String str)  
    {  
        return parseToDate(str, DATETIME_FORMAT_PATTERN);  
    }  
      
    /** 
     * 比较两个时间数相差多少分钟 
     * @param begin 
     * @param end 
     * @return 
     */  
    public static long getDiff(Date begin, Date end)  
    {  
        long diff = end.getTime() - begin.getTime();  
        diff = Math.abs(diff);  
          
        long day = diff / MILLIS_PER_DAY;// 计算差多少天  
        long hour = diff % MILLIS_PER_DAY / MILLIS_PER_HOUR;// 计算差多少小时  
        long min = diff % MILLIS_PER_DAY % MILLIS_PER_HOUR / MILLIS_PER_MINUTE;// 计算差多少分钟  
        long sec = diff % MILLIS_PER_DAY % MILLIS_PER_HOUR % MILLIS_PER_MINUTE / MILLIS_PER_SECOND;// 计算差多少秒  
          
        System.out.println("时间相差：" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒。");  
          
        return min;  
    }  
      
    /** 
     * 获得指定日期的前一天 
     *  
     * @param specifiedDay yy-MM-dd 
     * @return 
     * @throws Exception 
     */  
    public static String getBeforeDay(String specifiedDay)  
    {  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try  
        {  
            date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(specifiedDay);  
        }  
        catch (ParseException e)  
        {  
            e.printStackTrace();  
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day - 1);  
  
        String dayBefore = new SimpleDateFormat(DATE_FORMAT_PATTERN).format(c.getTime());  
        return dayBefore;  
    }  
  
    /** 
     * 获得指定日期的后一天 
     *  
     * @param specifiedDay yy-MM-dd 
     * @return 
     */  
    public static String getAfterDay(String specifiedDay)  
    {  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try  
        {  
            date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(specifiedDay);  
        }  
        catch (ParseException e)  
        {  
            e.printStackTrace();  
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day + 1);  
  
        String dayAfter = new SimpleDateFormat(DATE_FORMAT_PATTERN).format(c.getTime());  
        return dayAfter;  
    }  
  
       
    
    /**  
     * 获得一个月的所有日期
     * @param String 
     * @return string 
     * @throws ParseException 
     */    
    public static List<String> getList(String month) throws ParseException{
    	List<String> list= new ArrayList<String>();
    	String temp = getFirstDayOfMonth(month);
        Date date = getMonthStart(sdf.parse(temp));
        Date monthEnd = getMonthEnd(sdf.parse(temp));
        while (!date.after(monthEnd)) {
        	list.add(sdf.format(date));
            date = getNext(date);
        }
		return list;
    }
    
    private static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (1 - index));
        return calendar.getTime();
    }
    
 
    private static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, (-index));
        return calendar.getTime();
    }
 
    private static Date getNext(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    } 
    
    /**
     * 获取一个月的第一天
     * @param month
     * @return
     * @throws ParseException
     */
    public static String getFirstDayOfMonth(String  month) throws ParseException { 
    	DateFormat format1 = new SimpleDateFormat("yyyy-MM"); 
    	Date date = format1.parse(month);
        // 获取Calendar  
        Calendar calendar = Calendar.getInstance();  
        if(date != null)  
            calendar.setTime(date);  
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));  
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstDay= format.format(calendar.getTime());
        return  firstDay;
    }  
    
    /**  
     * 获得一个月的最后一天 
     * @param String 
     * @return string 
     * @throws ParseException 
     */    
    public static String getLastDayOfMonth(String month) throws ParseException {    
    	DateFormat format1 = new SimpleDateFormat("yyyy-MM"); 
    	Date date = format1.parse(month);
        // 获取Calendar  
        Calendar calendar = Calendar.getInstance();  
        if(date != null)  
            calendar.setTime(date);  
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));  
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay= format.format(calendar.getTime());
        return  lastDay;
    }
    public static void main(String[] args) {
		System.out.println(getWeekOfDate(new Date()));
	}
    /**   
     * 计算两个日期之间相差的天数   
     * @param d1 较小的时间  
     * @param d2  较大的时间  
     * @return 相差天数  
     * @throws ParseException   
     */      
    public static int daysBetween(Date d1, Date d2) throws ParseException  
    {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        d1 = sdf.parse(sdf.format(d1));  
        d2 = sdf.parse(sdf.format(d2));  
          
        Calendar cal = Calendar.getInstance();  
        cal.setTime(d1);  
        long time1 = cal.getTimeInMillis();  
        cal.setTime(d2);  
        long time2 = cal.getTimeInMillis();  
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);  
        return Integer.parseInt(String.valueOf(betweenDays));  
    }  
    /** 
     * 获得两个时间之间隔的天数 
     *  
     * @return 
     */  
    public static int daysBetween(String d1, String d2) throws ParseException  
    {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(sdf.parse(d1));  
        long time1 = cal.getTimeInMillis();  
        cal.setTime(sdf.parse(d2));  
        long time2 = cal.getTimeInMillis();  
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);  
          
        return Integer.parseInt(String.valueOf(betweenDays));  
    }
    
    /**
     * 获取当前日期是星期几<br>
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
    
}

